`
https://leetcode.cn/problems/surrounded-regions/
`

/**
 * @param {character[][]} board
 * @return {void} Do not return anything, modify board in-place instead.
 */
var solve = function (board) {
  const m = board.length, n = board[0].length

  const dfs = (i, j) => {
    if (
      i < 0 || i >= m ||
      j < 0 || j >= n ||
      board[i][j] === '#' ||
      board[i][j] === 'X'
    ) return

    board[i][j] = '#'

    dfs(i - 1, j)
    dfs(i + 1, j)
    dfs(i, j - 1)
    dfs(i, j + 1)
  }

  // 先把边缘的 O 以及跟边缘的 O 联通的所有 O 替换为 #
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      // 从边缘开始搜索
      const isEdge = i === 0 || j === 0 || i === m - 1 || j === n - 1;
      if (isEdge && board[i][j] === 'O') {
        dfs(i, j)
      }
    }
  }

  // 再把所有的 O 替换为 X，所有的 # 替换为 O
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      if (board[i][j] == 'O') {
        board[i][j] = 'X';
      }
      if (board[i][j] == '#') {
        board[i][j] = 'O';
      }
    }
  }
};